package com.four.dailymanage.repair.service;

import com.four.dailymanage.chamber.entity.Chamber;
import com.four.dailymanage.common.bean.Page;
import com.four.dailymanage.repair.dao.RepairDao;
import com.four.dailymanage.repair.entity.RepairApply;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class RepairApplyService {
    @Autowired
    RepairDao repairDao;
    //申请维修
    public boolean insert(RepairApply repairApply){
        if(repairApply==null){
            return false;
        }
        repairApply.setStatus("待接单");
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dataTimeString=sdf.format(new Date());
        repairApply.setRepairDt(dataTimeString);
        return repairDao.addRepair(repairApply)>0;
    }

    //分页
    public Page<RepairApply> getPageRepairApply (int pageNum, int pageSize, String sname, String content,String repairDt,String status,String repairMan){
        //计算起始位置
        int start=(pageNum-1)*pageSize;
        //获取分页的数据
        List<RepairApply> list=repairDao.getRepairApplyByCon(start,pageSize,sname,content,repairDt,status,repairMan);
        //获取总条目数
        int total=repairDao.countRepairApply(sname, content, repairDt,status,repairMan);
        //计算总页数
        int pages =(total % pageSize == 0 ? total / pageSize : total / pageSize + 1);
        //创建分页对象
        Page<RepairApply> page=new Page<>();
        page.setRecords(list);
        page.setTotal(total);
        page.setPages(pages);
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        return page;
    }

    //撤销
    public  boolean updateStatus(RepairApply repairApply){
        if (repairApply==null){
            return false;
        }
        repairApply.setStatus("已撤销");
        return repairDao.updateRepairApplyById(repairApply)>0;
    }

    //接单
    public  boolean updateReceive(RepairApply repairApply){
        if (repairApply==null){
            return false;
        }
        repairApply.setStatus("已接单");
        return repairDao.updateRepairApplyById(repairApply)>0;
    }
    //维修
    public boolean updateRepair(RepairApply repairApply){
        if (repairApply==null){
            return false;
        }
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dataTimeString=sdf.format(new Date());
        repairApply.setLoppyDt(dataTimeString);
        return  repairDao.updateRepairApply(repairApply)>0;
    }
    //退单
    public  boolean updateReject(RepairApply repairApply){
        if (repairApply==null){
            return false;
        }
        repairApply.setStatus("已退单");
        return repairDao.updateRepairApplyById(repairApply)>0;
    }

}
